import systemModel from "../../model/system/systemModel";
import loginModel from "../../model/login/loginModel";
const { uuid } = require('uuidv4');
const bcrypt = require('bcryptjs');

export default class systemService {

	/**
	 * ---------------------------------用户管理---------------------------------------
	 **/

	// 查询用户列表
	async getUserList(params) {
		let list = await new systemModel().getUserList(params);
		let count = await new systemModel().getUserCount(params);
		return {list,count}
	}
	// 保存或编辑用户
	async saveUser(params) {
		// 查询账号是否存在
		let res:any = await new systemModel().getUserByAccount(params);
		if(res.length){
			let msg = ''
			if(res[0].loginuser == params.loginuser){
				msg = '该账号已存在，请更换账号'
			}else if(res[0].telphone == params.telphone){
				msg = '该手机号已存在，请更换手机号'
			}
			return { msg }
		}
		if(params.id){
			//编辑
			delete params.roleName;
			delete params.createAt;
			delete params.loginTime;
			delete params.loginpwd;
			params.updateAt = new Date();
			return await new systemModel().editUser(params);
		}else{
			//保存
			params.id = uuid();
			params.createAt = new Date();
			// 生成一个随机的salt，通常建议使用genSaltSync(10)
			const salt = bcrypt.genSaltSync(10)
			// 使用bcrypt的hashSync方法将密码哈希化
			params.loginpwd = bcrypt.hashSync(params.loginpwd, salt)
			// 生成code
			if(params.cnum){
				for(let i = 0; i < params.cnum; i++){
					let item = {
						id: uuid(),
						userid: params.telphone,
						autocode: uuid(),
						isuse: 0,
						createAt: new Date(),
						status: 1
					}
					await new systemModel().createCode(item);
				}
			}
			return await new systemModel().saveUser(params);
		}
	}
	// 删除or禁用or启用
	async delUser(obj) {
		return await new systemModel().delUser(obj);
	}
	// 批量删除用户
	async delMoreUser(obj) {
		let { ids, status } = obj;
		let res;
		for(let id of ids){
			res = await new systemModel().delUser({id, status});
		}
		return res
	}
	 


	/**
	 * -------------------------------------角色管理----------------------------------------
	 **/
	// 查询角色列表
	async getRoleList(params) {
		let list = await new systemModel().getRoleList(params);
		let count = await new systemModel().getRoleCount(params);
		return {list,count}
	}
	// 角色列表不分页
	async getRoleListNopage() {
	  let res = await new systemModel().getRoleListNopage();
	  return res
	}
	// 保存或编辑角色
	async saveRole(params) {
		let checkedList = params.checkedList;   //选中的菜单
		if(params.id){
			//编辑
			params.updateAt = new Date();
			delete params.checkedList;
			delete params.createAt;
			// 编辑该角色
			await new systemModel().editRole(params);
			// 删除之前的配置
			await new systemModel().delRoleMenu(params.id);
		}else{
			//保存
			params.id = uuid();
			params.createAt = new Date();
			params.status = 1;
			delete params.checkedList;
			// 保存角色
			await new systemModel().saveRole(params);
		}
		// 保存该角色菜单权限
		for(let item of checkedList){
			let roleMenu = {
				id: uuid(),
				roleid: params.id,
				menuid: item.id,
				createAt: new Date(),
				status: 1
			}
			await new systemModel().saveRoleMenu(roleMenu);
		}
		return 'success'
	}
	// 删除角色
	async delRole(id) {
		return await new systemModel().delRole(id);
	}
	// 查询某个角色权限
	async getMenuRole(params) {
	  let res = await new systemModel().getMenuRole(params);
	  return res
	}



	/**
	 * ---------------------------------------菜单管理------------------------------------------
	 **/

	// list转tree
	transTree(data:any[]) {
		let result:any = [];
		let map:any = {};
		if (!Array.isArray(data)) {
			//验证data是不是数组类型
			return [];
		}
		data.forEach((item) => {
			//建立每个数组元素id和该对象的关系
			map[item.id] = item; //这里可以理解为浅拷贝，共享引用
		});
		data.forEach((item) => {
			let parent = map[item.pid]; //找到data中每一项item的爸爸
			if (parent) {
				//说明元素有爸爸，把元素放在爸爸的children下面
				(parent.children || (parent.children = [])).push(item);
			} else {
				//说明元素没有爸爸，是根节点，把节点push到最终结果中
				result.push(item); //item是对象的引用
			}
		});
		return result; //数组里的对象和data是共享的
	}
	// 菜单列表
	async getMenuList() {
		let roleMenu:any = await new systemModel().getMenuList();
		for(let menu of roleMenu){
				let meta:any = await new loginModel().getMeta(menu.id);
				if(meta.length){
						menu.meta = meta[0];
						menu.label = meta[0].title;
						menu.title = meta[0].title;
				}
		}
		let treeList = await this.transTree(roleMenu);
		return treeList;
	}
	// 添加菜单
	async saveMenu(params) {
		if(params.id){
		    //编辑
				let meta = params.meta;
				let obj = {
					id: params.id,
					pid: params.menuSuperior.length ? params.menuSuperior[params.menuSuperior.length-1] : '',
					path: params.path,
					name: params.name,
					component: params.component,
					redirect: params.redirect,
					sort: params.sort,
					menuSuperior: params.menuSuperior?.toString(),
					menuType: params.menuType,
					btnPower: params.btnPower,
					updateAt: new Date(),
					createBy: params.createBy,
				}
				// 编辑menu
				await new systemModel().editMenu(obj);
				// 编辑menu-meta
				meta.menuid = params.id;
				meta.updateAt = new Date();
				delete meta.createAt;
		    return await new systemModel().editMenuMeta(meta);
		}else{
		    //保存
				let meta = params.meta;
		    params.id = uuid();
				params.pid = params.menuSuperior.length ? params.menuSuperior[params.menuSuperior.length-1] : '';
		    params.createAt = new Date();
		    params.status = 1;
				params.menuSuperior = params.menuSuperior?.toString();
				delete params.isLink;
				delete params.meta;
				// 保存menu
				await new systemModel().saveMenu(params);
				// 保存menu-meta
				meta.id = uuid();
				meta.menuid = params.id;
				meta.createAt = new Date();
				meta.status = 1;
		    return await new systemModel().saveMenuMeta(meta);
		}
	}
	// 删除菜单
	async delMenu(id) {
		return await new systemModel().delMenu(id);
	}



	/**
	 * -----------------------------------------一级代理商---------------------------------------------
	 **/
	
	// 列表
	async getCompanyList(params) {
		let list = await new systemModel().getCompanyList(params);
		let count = await new systemModel().getCompanyCount(params);
		return {list,count}
	}
	// 保存或编辑
	async saveCompany(params) {
	    if(params.id){
	        //编辑
	        delete params.createAt;
	        params.updateAt = new Date();
	        return await new systemModel().editCompany(params);
	    }else{
	        //保存
	        params.id = uuid();
	        params.createAt = new Date();
	        params.status = 1;
	        return await new systemModel().saveCompany(params);
	    }
	}
	// 删除
	async delCompany(id) {
		return await new systemModel().delCompany(id);
	}
	// 列表，不分页，查全部
	async getCompanyListNopage(params) {
		return await new systemModel().getCompanyList(params);
	}

	
	/**
	 * -------------------------------------授权码----------------------------------------
	 **/
	// 列表
	async getCodeList(params) {
		let list = await new systemModel().getCodeList(params);
		let count = await new systemModel().getCodeCount(params);
		return {list,count}
	}


	/**
	 * ---------------------------------电脑主机管理---------------------------------------
	 **/

	// 列表
	async getComputerList(params) {
		let list = await new systemModel().getComputerList(params);
		let count = await new systemModel().getComputerCount(params);
		return {list,count}
	}
	// 审核
	async examineComputer(params) {
		return await new systemModel().examineComputer(params);
	}
	// 延期
	async extensionComputer(params) {
		return await new systemModel().extensionComputer(params);
	}


	
	/**
	 * -----------------------------------------账号管理---------------------------------------------
	 **/
	
	// 列表
	async getAccountList(params) {
		let list = await new systemModel().getAccountList(params);
		let count = await new systemModel().getAccountCount(params);
		return {list,count}
	}
	// 保存或编辑
	async saveAccount(params) {
	    if(params.id){
	        //编辑
	        delete params.createAt;
	        params.updateAt = new Date();
	        return await new systemModel().editAccount(params);
	    }else{
	        //保存
	        params.id = uuid();
	        params.createAt = new Date();
	        params.status = 1;
	        return await new systemModel().saveAccount(params);
	    }
	}
	// 删除
	async delAccount(id) {
		return await new systemModel().delAccount(id);
	}


	/**
	 * -----------------------------------------ip代理管理---------------------------------------------
	 **/
	
	// 列表
	async getIpProxyList(params) {
		let list = await new systemModel().getIpProxyList(params);
		let count = await new systemModel().getIpProxyCount(params);
		return {list,count}
	}
	// 保存或编辑
	async saveIpProxy(params) {
	    if(params.id){
	        //编辑
	        delete params.createAt;
	        params.updateAt = new Date();
	        return await new systemModel().editIpProxy(params);
	    }else{
	        //保存
	        params.id = uuid();
	        params.createAt = new Date();
	        params.status = 1;
	        return await new systemModel().saveIpProxy(params);
	    }
	}
	// 删除
	async delIpProxy(id) {
		return await new systemModel().delIpProxy(id);
	}


	/**
	 * -----------------------------------------任务管理---------------------------------------------
	 **/
	
	// 列表
	async getTaskList(params) {
		let list = await new systemModel().getTaskList(params);
		let count = await new systemModel().getTaskCount(params);
		return {list,count}
	}
	// 保存或编辑
	async saveTask(params) {
	    if(params.id){
	        //编辑
	        delete params.createAt;
	        params.updateAt = new Date();
	        return await new systemModel().editTask(params);
	    }else{
	        //保存
	        params.id = uuid();
	        params.createAt = new Date();
	        params.status = 1;
	        return await new systemModel().saveTask(params);
	    }
	}
	// 删除
	async delTask(id) {
		return await new systemModel().delTask(id);
	}
    

	/**
	 * -----------------------------------------工会管理---------------------------------------------
	 **/
	
	// 列表，不分页，查全部
	async getLaborunionListNopage() {
		return await new systemModel().getLaborunionListNopage();
	}
    
}